﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Data.SqlClient;

namespace Privosoft.DataFrame
{   
    
    class Program
    {        
        static string errFileName = null;
        
        private static bool IsKey(string s)
        {
            return s.Length >= 2 && (s[0] == '/' || s[0] == '-');
        }

        private static string GetArg(string[] args, int argIndex, string param)
        {
            int idx = argIndex + 1;
            if ((idx >= args.Length) || (IsKey(args[idx])))
            {
                throw new LoaderException(String.Format("-{0} parameter value not provided", param));
            }
            else
                return args[idx];
        }

        static void Main(string[] args)
        {
            string inpFileName = null;
            string schema = null;
            string dbSchema = null;

            try
            {
                var argErrors = new List<LoaderException>();

                for (int i = 0; i < args.Length; i++)
                {
                    try
                    {
                        string s = args[i];
                        string sKey = "";
                        if (IsKey(s))
                        {
                            sKey = s[1].ToString();
                            sKey = sKey.ToUpper();
                            string sVal = s.Length > 2 ? s.Substring(2, s.Length - 2) : "";
                            switch (sKey)
                            {
                                case "I":
                                    inpFileName = GetArg(args, i, "i");
                                    break;
                                case "E":
                                    errFileName = GetArg(args, i, "e");
                                    Logger.Create(errFileName);
                                    break;
                                case "S":
                                    schema = GetArg(args, i, "s");
                                    break;
                                case "D":
                                    dbSchema = GetArg(args, i, "d");
                                    break;
                                default: 
                                    throw new LoaderException("Invalid argument '" + sKey + "'");
                            }
                        }
                    }
                    catch (LoaderException ex)
                    {
                        argErrors.Add(ex);
                    }
                }
                if (File.Exists(errFileName))
                {
                    File.Delete(errFileName);
                }
                foreach (LoaderException ex in argErrors)
                {
                    throw ex;
                }

                // 1
                if (schema == null)
                {
                    throw new LoaderException("-s parameter not provided");
                }
                if (inpFileName == null)
                {
                    throw new LoaderException("-i parameter not provided");
                }
                // 2 
                if (!File.Exists(inpFileName))
                {
                    throw new LoaderException(String.Format("File {0} does not exist", inpFileName));
                }
                // 3
                var cn = new SqlConnection(Properties.Settings.Default.DBConnectionString);
                using (var db = new DbContext(cn, dbSchema))
                {
                    SchemaInfo sch = db.GetSchemaByName(schema);
                    if (sch == null)
                        throw new LoaderException(String.Format("Schema {0} is not set up", schema));
                    
                    db.LoadSchemaColumns(sch);
                    // 4
                    if (sch.Columns.Count == 0)
                        throw new LoaderException(String.Format("Scheme {0} is not set up properly. Please add columns", schema));
                    
                    FileReader.ImportCsv(inpFileName, sch, db);
                }

                // 4
                if (dbSchema == null)
                {
                    throw new LoaderException("-d parameter not provided");
                }
            }
            catch (LoaderException ex)
            {
                Logger.Log(LogLevels.FATAL, ex.Error.Row, ex.Message, ex.Error.Column);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevels.FATAL, 0, ex.Message, null);
            }
        }
    }
}
